package com.ruoyi.part.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SerialNumberGenerator;
import com.ruoyi.part.domain.DmsPartStorageInfo;
import com.ruoyi.part.mapper.DmsPartStorageInfoMapper;
import com.ruoyi.part.service.IDmsPartStorageInfoService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 零件库存信息Service业务层处理
 * 
 * @author liuchuanfa
 * @date 2024-03-17
 */
@Service
public class DmsPartStorageInfoServiceImpl extends ServiceImpl<DmsPartStorageInfoMapper,DmsPartStorageInfo> implements IDmsPartStorageInfoService
{
    @Autowired
    private DmsPartStorageInfoMapper dmsPartStorageInfoMapper;
    @Autowired
    SerialNumberGenerator serialNumberGenerator;
    @Autowired
    private ISysDeptService iSysDeptService;

    /**
     * 查询零件库存信息
     * 
     * @param id 零件库存信息主键
     * @return 零件库存信息
     */
    @Override
    public DmsPartStorageInfo selectDmsPartStorageInfoById(Long id)
    {
        return dmsPartStorageInfoMapper.selectDmsPartStorageInfoById(id);
    }

    /**
     * 查询零件库存信息列表
     * 
     * @param dmsPartStorageInfo 零件库存信息
     * @return 零件库存信息
     */
    @Override
    public List<DmsPartStorageInfo> selectDmsPartStorageInfoList(DmsPartStorageInfo dmsPartStorageInfo)
    {
        return dmsPartStorageInfoMapper.selectDmsPartStorageInfoList(dmsPartStorageInfo);
    }

    /**
     * 新增零件库存信息
     * 
     * @param dmsPartStorageInfo 零件库存信息
     * @return 结果
     */
    @Override
    public void insertDmsPartStorageInfo(DmsPartStorageInfo dmsPartStorageInfo)
    {
        //限制新增状态只能是：厂端在库
        int partStatus = dmsPartStorageInfo.getPartStatus();
        if (partStatus != 0){
            throw new ServiceException("新增零件状态只能为厂端在库！");
        }
        //新增时设置库存类型为厂端
        dmsPartStorageInfo.setInventoryType("厂端");
        //当用户没填零件代码时自动生成
        if (StrUtil.isBlank(dmsPartStorageInfo.getPartNo())){
            dmsPartStorageInfo.setPartNo(serialNumberGenerator.generateSerialNumber("LJKC"));
        }
        //填充厂端id和名称
        dmsPartStorageInfo.setFactoryId(SecurityUtils.getLoginUser().getUser().getDept().getParentId());
        dmsPartStorageInfo.setFactoryName(iSysDeptService.selectDeptById(SecurityUtils.getLoginUser().getUser().getDept().getParentId()).getDeptName());

        save(dmsPartStorageInfo);

    }

    /**
     * 修改零件库存信息
     * 
     * @param dmsPartStorageInfo 零件库存信息
     * @return 结果
     */
    @Override
    public void updateDmsPartStorageInfo(DmsPartStorageInfo dmsPartStorageInfo)
    {
        updateById(dmsPartStorageInfo);
    }

    /**
     * 批量删除零件库存信息
     * 
     * @param ids 需要删除的零件库存信息主键
     * @return 结果
     */
    @Override
    public void deleteDmsPartStorageInfoByIds(Long[] ids)
    {
        dmsPartStorageInfoMapper.deleteDmsPartStorageInfoByIds(ids);
    }

    /**
     * 删除零件库存信息信息
     * 
     * @param id 零件库存信息主键
     * @return 结果
     */
    @Override
    public void deleteDmsPartStorageInfoById(Long id)
    {
        dmsPartStorageInfoMapper.deleteDmsPartStorageInfoById(id);
    }
}
